package com.wft.chapter15;

import java.nio.CharBuffer;

public class BufferTest {
    public static void main(String[] args) {
        // 创建buffer
        System.out.println("初始化...");
        CharBuffer buffer = CharBuffer.allocate(8);
        System.out.println("capacity:" + buffer.capacity());
        System.out.println("limit:" + buffer.limit());
        System.out.println("postion:" + buffer.position());
        System.out.println("***************************");

        // 放入元素
        buffer.put('w');
        buffer.put('f');
        buffer.put('t');
        System.out.println("放入3个元素后...");
        System.out.println("capacity:" + buffer.capacity());
        System.out.println("limit:" + buffer.limit());
        System.out.println("postion:" + buffer.position());
        System.out.println("***************************");

        // flip后
        buffer.flip();
        System.out.println("flip后...");
        System.out.println("capacity:" + buffer.capacity());
        System.out.println("limit:" + buffer.limit());
        System.out.println("postion:" + buffer.position());
        System.out.println("***************************");

        System.out.println("取出一个元素：" + buffer.get());
        System.out.println("capacity:" + buffer.capacity());
        System.out.println("limit:" + buffer.limit());
        System.out.println("postion:" + buffer.position());
        System.out.println("***************************");

        buffer.clear();
        System.out.println("clear后...");
        System.out.println("capacity:" + buffer.capacity());
        System.out.println("limit:" + buffer.limit());
        System.out.println("postion:" + buffer.position());
        System.out.println("***************************");

        // clear后元素buffer对象总的元素依然存在
        System.out.println("clear后取出一个元素：" + buffer.get(2));
        System.out.println("capacity:" + buffer.capacity());
        System.out.println("limit:" + buffer.limit());
        System.out.println("postion:" + buffer.position());
        System.out.println("***************************");
    }
}
